package cn.wizzer.app.task.commons.ext.quartz.job;

import cn.wizzer.app.user.modules.models.UserGiftOrder;
import cn.wizzer.app.user.modules.services.GiftOrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

import java.util.List;

@IocBean
public class GiftOrderJob implements Job {

    private static final Log log = Logs.get();

    @Inject
    @Reference(retries = 0, timeout = 500000)
    private GiftOrderService giftOrderService;

    @Override
    public void execute(JobExecutionContext context) {
        try {

            Cnd cnd = Cnd.where("sum_to_balance", "=", 0);
            cnd.limit(0,1000);
            boolean flag = true;
            while (flag) {
                List<UserGiftOrder> query = giftOrderService.query(cnd);
                query.forEach(userGiftOrder -> {
                    giftOrderService.settlement(userGiftOrder.getId());
                });
                if (query.size()<1000) {
                    flag = false;
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

}
